package com.htl.controller;

import com.htl.exception.Assert;
import com.htl.result.ResponseEnum;
import com.htl.service.SmsService;
import com.htl.util.RandomUtil;
import com.htl.util.RegexValidateUtil;
import com.htl.util.ResultVOUtil;
import com.htl.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sms")
public class SmsController {

    @Autowired
    private SmsService smsService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/send/{mobile}")
    public ResultVO send(@PathVariable("mobile") String mobile){
        Assert.notNull(mobile, ResponseEnum.PARAMETER_NULL);
        Assert.isTrue(RegexValidateUtil.checkMobile(mobile), ResponseEnum.MOBILE_ERROR);
        String code = RandomUtil.getSixBitRandom();
        boolean send = this.smsService.send(mobile, code);
        if(send) {
            this.redisTemplate.opsForValue().set("uushop-sms-code-"+mobile, code);
            return ResultVOUtil.success("短信发送成功！");
        }
        return ResultVOUtil.fail(null);
    }

}
